package com.ruoyi.web.controller.kaoyan;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kaoyan.domain.EducationForumComment;
import com.ruoyi.kaoyan.service.EducationForumCommentService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 前台评论类
 * @author carpediem
 */
@Api(tags = "前台评论", value = "前台评论")
@RestController
@RequestMapping("/v2/comment")
public class CommentApi extends BaseController {

    @Autowired
    private EducationForumCommentService commentService;
    @Autowired
    private ISysUserService userService;

    @ApiOperation("评论列表")
    @PostMapping("/list")
    public AjaxResult commentList(Long postId){
        LambdaQueryWrapper<EducationForumComment> eq = new LambdaQueryWrapper<EducationForumComment>()
                .eq(true, EducationForumComment::getVerFlag, 1)
                .eq(StringUtils.isNotNull(postId), EducationForumComment::getPostId, postId);
        Page<EducationForumComment> page = commentService.page(startPagePlus(), eq);
        List<EducationForumComment> records = page.getRecords();
        Set<Long> collect = records.stream().map(EducationForumComment::getUserId).collect(Collectors.toSet());
        List<SysUser> sysUsers = userService.list(new LambdaQueryWrapper<SysUser>().in(StringUtils.isNotEmpty(collect), SysUser::getUserId, collect));
        Map<Long, String> map = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getUserName));
        Map<Long, List<EducationForumComment>> listMap = records.stream().collect(Collectors.groupingBy(EducationForumComment::getParentId));
        records.forEach(e -> {
            e.setUserName(map.get(e.getUserId()));
            e.setSonCommentList(listMap.get(e.getId()));
        });
        return AjaxResult.ok().page(page, records).build();
    }

    @ApiOperation("添加评论")
    @PostMapping("/add")
    public AjaxResult addComment(@RequestBody EducationForumComment comment){
        if (StringUtils.checkObjAllFieldsIsNull(comment)){
            return AjaxResult.warn("评论内容为空");
        }
        boolean save = commentService.save(comment);
        return toAjax(save);
    }

    @ApiOperation("评论点赞")
    @PostMapping("/like")
    public AjaxResult like(Long id){
        Long likeSum = commentService.getById(id).getLikeSum();
        EducationForumComment comment = EducationForumComment.builder()
                .likeSum(likeSum + 1)
                .build();
        boolean save = commentService.save(comment);
        return toAjax(save);
    }
}
